package site.shihuan.helpdesk.security.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import site.shihuan.helpdesk.common.exception.CommonException;
import site.shihuan.helpdesk.common.model.Constants;
import site.shihuan.helpdesk.common.model.RestResult;
import site.shihuan.helpdesk.common.model.RestResultUtils;

/**
 * TODO
 *
 * @author 周世焕
 * @Date 2021/1/27 13:22
 */
@RestControllerAdvice
@Slf4j
public class GlobalHandler {

    @ExceptionHandler(CommonException.class)
    @ResponseBody
    public RestResult handleCommonException(CommonException commonException) {
        log.debug("CommonException: {}" , commonException.toString());
        return RestResultUtils.failedWithMsg(commonException.getErrCode(),commonException.getErrMsg());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public RestResult handleAllException(Exception exception) {
        log.debug("Exception: {}" , exception);
        return RestResultUtils.failedWithMsg(Constants.INTERNAL_SERVER_ERR,exception.getMessage());
    }
}
